package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: Cherry
 * @create: 2021-09-15 10:11
 * @description: 统一返回
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultData<T> {

    /** 结果状态 ,具体状态码参见ResultData.java*/
    private int code;
    private String message;
    private T data;
    //private long timestamp ;


    /**
     * 成功返回格式
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCode.RC100.getCode());
        resultData.setMessage(ReturnCode.RC100.getMessage());
        resultData.setData(data);
        return resultData;
    }

    /**
     * 自定义成功返回格式
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> success(String message,T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCode.RC100.getCode());
        resultData.setMessage(message);
        resultData.setData(data);
        return resultData;
    }

    /**
     * 自定义成功返回格式
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> success(Integer code, String message, T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        resultData.setData(data);
        return resultData;
    }

    /**
     * 失败返回格式
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> fail(int code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        return resultData;
    }

    /**
     * 失败返回格式 携带data
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> fail(int code, String message,T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        resultData.setData(data);
        return resultData;
    }
}
